import 'dart:async';

import 'package:flutter/material.dart';

class TimerCountDown {
  static TimerCountDown? _instance;
  Timer? _timer;

  TimerCountDown._internal();

  int _timerCount = 3;

  void setTimerCount(int count) {
    _timerCount = count;
  }

  factory TimerCountDown() {
    _instance ??= TimerCountDown._internal();
    return _instance!;
  }

  void startTimer(VoidCallback callback) {
    /// 保证timer有实例的时候
    if (_timer != null) return;
    _timer = Timer.periodic(const Duration(seconds: 1), (_) {
      _timerCount--;
      if (_timerCount == 0) {
        callback();
        destroyTimer();
      }
    });
  }

  void destroyTimer() {
    _timer?.cancel();
    _timer = null;
  }
}
